<div ng-controller="ProviderCtrl">
  <div class="toolbar">
    <i class="feature-name"><a name="data-bind">Run block</a></i>
    <i class="fa fa-star fa-lg" tooltip="difficulty coefficient"></i>
  </div>
  <div class="content">
    <div class="example" ng-class="{'hidden': showCode}">
      <div class="col-md-7 boilerplate">
        <h2 class="crosshead"><span class="label label-primary">Example of run block</span></h2>
        <pre>
          <code class="javascript">
// You can have as many of these as you want.
// You can only inject instances (not Providers)
app.run(function($rootScope, AuthService) {

  $rootScope.$on('$routeChangeStart',function(evt, next, current) {
    // If the user is NOT logged in
    if (!AuthService.userLoggedIn()) {
      if (next.templateUrl === "login.html") {
        // Already heading to the login route so no need to redirect
      } else {
        $location.path('/login');
      }
    }
  });
})
.run(function($httpBackend) {
    var comments = [
    {
      'id': 1,
      'username': 'Chen Jian',
      'time': '04/Mar/15 9:05 AM',
      'content': 'Goo post, save my whole day',
    },
    {
      'id': 2,
      'username': 'Wang Lin',
      'time': '02/Mar/15 9:05 AM',
      'content': 'I got a waring when run your code'
    }
  ];

  $httpBackend.whenGET('/comments').respond(function(method, url, data) {
    return [200, comments, {}];
  });

  // this is the creation of a new resource
  $httpBackend.whenPOST('/comments').respond(function(method, url, data) {
    var params = angular.fromJson(data);
    params.id = newId();
        
    return [201, params, {}];
  });

  // Don't mock the html views
  $httpBackend.whenGET(/views\/\w+.*/).passThrough();

  // For everything else, don't mock
  $httpBackend.whenGET(/^\w+.*/).passThrough();
  $httpBackend.whenPOST(/^\w+.*/).passThrough();

})

          </code>
        </pre>
      </div>
      <div class="col-md-5">
        <h2><span class="label label-primary">Instruction</span></h2>
        <ul class="instruction">
          <li>The run function is useful to perform any kind of initialization after the objects instances registered in the module have already been created.</li>
          <li>Typically, this is where we can set up handlers for some events that happen at the global scale of the app.</li>
          <li>For example,  we wanna run a function that validates that we have an authenticated user every time we change route</li>
          <li>Especially, we could mock backend response in this region.</li>
        </ul>
      </div>
    </div>
  </div>
</div>